package com.newbee.multchat;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.Scroller;

import com.hhu.newbee.R;
import com.newbee.app.OnRefrushActivity;
import com.newbee.fragement.FriendFragment;
import com.newbee.fragement.multchat.MultChatFragement;
import com.newbee.fragement.multchat.MultChatResultFragement;
import com.newbee.fragement.multchat.MultChatSendMailChooseFriendFragement;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceKey;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;
import com.newbee.smack.SmackClient;
import com.newbee.viewpager.RegViewPager;
import com.newbee.viewpager.trans.ZoomOutPageTransformer;

public class MultChatActivity extends FragmentActivity
{

	private RegViewPager mViewPager;
	private List<Fragment> mFragments;
	private FragmentPagerAdapter mFragmentPagerAdapter;
	private String mRoomName;

	private MultChatResultFragement mResultFragement;
	private MultChatSendMailChooseFriendFragement mSendMailFriendFragement;

	private String mMultResult;

	public static final String INIT_TAG = "INIT_TAG";
	public static final String MULT_RESULT = "MULT_RESULT";

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_reg);
		mResultFragement = new MultChatResultFragement();
		mSendMailFriendFragement = new MultChatSendMailChooseFriendFragement();
		initViewPager();
		Bundle bundle = this.getIntent().getExtras();
		mRoomName = (String) bundle.get(MultChatFragement.CHAT_ROOM_MAME);
		int init_tag = bundle.getInt(INIT_TAG, -1);
		if (init_tag != -1)
		{
			goToNextPager(2);

			mMultResult = bundle.getString(MULT_RESULT);

		}

	}

	private void initViewPager()
	{
		this.mViewPager = (RegViewPager) this.findViewById(R.id.viewpager_reg);
		this.mFragments = new ArrayList<Fragment>();

		this.mFragments.add(new MultChatFragement());
		this.mFragments.add(mResultFragement);
		this.mFragments.add(mSendMailFriendFragement);
		changeViewPageScroller();
		mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
		mFragmentPagerAdapter = new FragmentPagerAdapter(
				getSupportFragmentManager())
		{

			@Override
			public int getCount()
			{
				// TODO Auto-generated method stub
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int posi)
			{
				// TODO Auto-generated method stub
				return mFragments.get(posi);
			}
		};
		mViewPager.setAdapter(mFragmentPagerAdapter);

	}

	public void goToNextPager(int nextIndex)
	{

		switch (nextIndex)
		{
		case 1:

		{
			Map<String, Object> paras = new HashMap<String, Object>();

			paras.put(MainServiceKey.TRANS_TAG, this.mRoomName);

			MainServiceTask mst = new MainServiceTask(TaskType.TRANS, paras,
					MultChatResultFragement.class.getName());
			MainService.addActivityMap(MultChatResultFragement.class.getName(),
					mResultFragement);
			MainService.addTaskQueue(mst);
			mResultFragement.showProgressDlg();
			break;
		}
		case 2:
		{
			MainServiceTask mst = new MainServiceTask(TaskType.GET_FRIEND,
					null, MultChatSendMailChooseFriendFragement.class.getName());
			MainService.addActivityMap(
					MultChatSendMailChooseFriendFragement.class.getName(),
					mSendMailFriendFragement);
			MainService.addTaskQueue(mst);
			break;
		}

		default:
			break;
		}

		mViewPager.setCurrentItem(nextIndex);
	}

	private void changeViewPageScroller()
	{
		try
		{
			Field mField = ViewPager.class.getDeclaredField("mScroller");
			mField.setAccessible(true);
			FixedSpeedScroller scroller;
			scroller = new FixedSpeedScroller(this,
					new AccelerateDecelerateInterpolator());
			mField.set(mViewPager, scroller);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

	}

	@Override
	protected void onDestroy()
	{
		MainService.removeActivity(MultChatFragement.class.getName());
		SmackClient.removeMultChat(this.mRoomName);
		MainService.removeActivity(MultChatResultFragement.class.getName());
		MainService.removeActivity(MultChatActivity.class.getName());
		super.onDestroy();
	}

	public String getmMultResult()
	{
		return mMultResult;
	}

	public void setmMultResult(String mMultResult)
	{
		this.mMultResult = mMultResult;
	}

}

class FixedSpeedScroller extends Scroller
{
	private int mDuration = 800;

	public FixedSpeedScroller(Context context)
	{
		super(context);
	}

	public FixedSpeedScroller(Context context, Interpolator interpolator)
	{
		super(context, interpolator);
	}

	@Override
	public void startScroll(int startX, int startY, int dx, int dy, int duration)
	{
		super.startScroll(startX, startY, dx, dy, mDuration);
	}

	@Override
	public void startScroll(int startX, int startY, int dx, int dy)
	{
		super.startScroll(startX, startY, dx, dy, mDuration);
	}

	public void setmDuration(int time)
	{
		mDuration = time;
	}

	public int getmDuration()
	{
		return mDuration;
	}

};